package com.evenvi.bio;

import java.net.ServerSocket;
import java.net.Socket;

public class TimeServer {
    private int port;

    public TimeServer(int port){
        this.port = port;
    }

    public void start() throws Exception{
        ServerSocket server = null;
        try {
            server = new ServerSocket(port);
            System.out.println("The time server start in port: " + port);
            Socket socket = null;

            //创建i/o任务线程池
            TimeServerHandlerExecutePool singleExecutor = new TimeServerHandlerExecutePool(
                    50,
                    10000
            );

            while (true){
                socket = server.accept();
                singleExecutor.execute(new TimeServerHandler(socket));
            }

        }finally {
            if(server != null){
                System.out.println("The time server close");
                server.close();
                server = null;
            }
        }
    }

    public static void main(String[] args) throws Exception{
        if(args.length != 1)
        {
            System.out.println("Sage: " + TimeServer.class.getSimpleName() + " <port>");
            return;
        }

        int port = Integer.valueOf(args[0]);
        new TimeServer(port).start();

    }
}
